package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ttg.common.entities.BasePage;
import com.ttg.model.dto.QMReportDTO;
import com.ttg.model.mapper.QualityManageIndustryReportMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.model.pojo.QualityManageIndustryReport;
import com.ttg.model.pojo.QualityManageLadderRule;
import com.ttg.model.vo.QMIndustryReportVO;
import com.ttg.web.dao.QualityManageIndustryReportDao;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * (TblQualityManageIndustryReport) daoImpl
 *
 * @author chenyou
 * @datetime 2025-08-18 14:50:33
 */
@Service
public class QualityManageIndustryReportDaoImpl extends ServiceImpl<QualityManageIndustryReportMapper, QualityManageIndustryReport> implements QualityManageIndustryReportDao {



    @Override
    public QualityManageIndustryReport getByUnique(String year,String month, String clsId1, String clsId2) {
        LambdaQueryWrapper<QualityManageIndustryReport> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(QualityManageIndustryReport::getRepYear,year)
                .eq(QualityManageIndustryReport::getRepMonth,month)
                .eq(QualityManageIndustryReport::getRepClsId1,clsId1)
                .eq(QualityManageIndustryReport::getRepClsId2,clsId2);
        return getOne(wrapper);
    }

    @Override
    public void saveOrUpdateBatch(List<QualityManageIndustryReport> reports) {
        for(QualityManageIndustryReport report : reports){
            QualityManageIndustryReport old = getByUnique(report.getRepYear(),report.getRepMonth(),report.getRepClsId1(),report.getRepClsId2());
            if(Objects.isNull(old)){
                save(report);
            }else{
                report.setRepId(old.getRepId());
                updateById(report);
            }
        }
    }

    @Override
    public void saveOrUpd(QualityManageIndustryReport report) {
        QualityManageIndustryReport old = getByUnique(report.getRepYear(),report.getRepMonth(),report.getRepClsId1(),report.getRepClsId2());
        if(Objects.isNull(old)){
            save(report);
        }else{
            report.setRepId(old.getRepId());
            updateById(report);
        }
    }

    @Override
    public BasePage<QMIndustryReportVO> report(BasePage page,QMReportDTO reqDto) {
        return baseMapper.report(page,reqDto);
    }

    @Override
    public List<String> selectYears() {
        return baseMapper.selectYears();
    }

    @Override
    public List<String> selectMonths(String year) {
        return baseMapper.selectMonths(year);
    }
}

